package admin

import (
	"context"
	"fmt"

	"oil-station-backend/internal/svc"
	"oil-station-backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateFuelPriceLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateFuelPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateFuelPriceLogic {
	return &UpdateFuelPriceLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateFuelPriceLogic) UpdateFuelPrice(req *types.UpdateFuelPriceReq) (resp *types.FuelType, err error) {
	// 验证油品价格
	if req.Price <= 0 {
		return nil, fmt.Errorf("油品价格必须大于0")
	}

	// 更新油品价格
	fuelType, err := l.svcCtx.FuelTypeModel.UpdatePrice(req.Id, req.Price)
	if err != nil {
		return nil, err
	}

	// 转换为API响应格式
	return &types.FuelType{
		Id:    fuelType.Id,
		Type:  fuelType.Type,
		Name:  fuelType.Name,
		Price: fuelType.Price,
	}, nil
}